<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        ref="loginFormRef"
        :rules="rules"
        class="login-form"
        label-width="80px"
        :model="loginForm"
      >
        <el-form-item label="用户名">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-user"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        // name: [
        //   { required: true, message: "请输入正确用户名", trigger: "blur" },
        //   {
        //     min: 3,
        //     max: 10,
        //     message: "长度在 3 到 10 个字符",
        //     trigger: "blur",
        //   },
        // ],
        password: [
          { required: true, message: "请输入正确格式的密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error("登陆失败");
          return;
        } else {
          const { data: res } = await this.$http.post("login", this.loginForm);
          console.log(res);
          if (res.meta.status == 200) {
            this.$router.push("/home");
            this.$message.success("登陆成功");
            window.sessionStorage.setItem("token", res.data.token);
          } else if (res.meta.status !== 200) {
            this.$message.error("用户名错误或密码错误!");
          }
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  background-color: #ddd;
  height: 100%;
  position: relative;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 10px;
  position: absolute;
  left: calc(50% - 225px);
  top: calc(50% - 150px);
}
img {
  width: 100%;
  height: 100%;
}
.avatar-box {
  width: 130px;
  height: 130px;
  border: 5px solid #ddd;
  border-radius: 50%;
  overflow: hidden;
  position: absolute;
  left: calc(50% - 75px);
  top: -50px;
  background-color: #fff;
}
.btns {
  display: flex;
  justify-content: space-around;
}
.login-form {
  position: absolute;
  bottom: 0;
  /* margin: 5%; */
  padding: 5%;
  width: 100%;
  box-sizing: border-box;
}
</style>